import {ethers} from "ethers"

const ALCHEMY_MAINNET_URL = 'https://eth-mainnet.g.alchemy.com/v2/mkM5-GVJCs00ghRsjT7aU';
// 连接主网 provider
const provider = new ethers.JsonRpcProvider(ALCHEMY_MAINNET_URL);

// USDT的合约地址
const contractAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7'

const abi = [
    "event Transfer(address indexed from, address indexed to, uint value)"
]
// 创建合约对象-只读
const contract = new ethers.Contract(contractAddress, abi, provider)

// 监听一次Transfer事件
contract.once("Transfer", (from, to, value)=>{
    console.log(`${from} -> ${to} ${ethers.formatUnits(ethers.getBigInt(value), 6)}`)
})

// 持续监听Transfer事件
contract.on("Transfer", (from, to, value)=>{
    console.log(`${from} -> ${to} ${ethers.formatUnits(ethers.getBigInt(value), 6)}`)
})